import { ActionType, PageContainer, ProColumns, ProTable } from '@ant-design/pro-components'
import { Button, Divider, Modal, message } from 'antd';
import React, { useRef, useState } from 'react'
import { InfoCircleTwoTone } from '@ant-design/icons';
import { get_sickbed_list, quash_sickbed } from '@/services/sickbeds';
import { useModel } from '@umijs/max';
import SickbedsInfo from '../EstablishedSickbeds/components/SickbedsInfo';

type GithubIssueItem = {
    patient_name: any;
    phone: string;
    id: number;
    name: string;
    sex: string;
    birth: string;
    id_card: string;
    address: number;
    create_time: string;
    bind_doctor: [{ id: number; username: string }];
    info_status: number;
    province: string;
    city: string;
    area: string;
    family_address: string;
};
const MySickbeds = () => {
    const actionRef = useRef<ActionType>();
    const { initialState, setInitialState } = useModel('@@initialState');
    // 存储当前id
    const [id, setId] = useState<number>();
    // 存储详情模态框的状态
    const [isInfoModalOpen, setIsInfoModalOpen] = useState(false);
    // 控制撤床模态框的状态
    const [delVisible, setDelVisible] = useState(false);

    // 控制详情模态框的显示与隐藏
    const isShowInfoModal = (status: boolean) => {
        setIsInfoModalOpen(status)
    }

    // 获取我负责的家庭病床列表
    const getMySickBeds = async (params: {
        pageSize?: number | undefined;
        current?: number | undefined;
        keyword?: string | undefined;
    }) => {
        //默认分页参数和后端接口字段不匹配
        let request_body = {
            ...params,
            doctor_id: initialState?.currentUser?.id,
            current: params.current,
            pageSize: params.pageSize,
        };
        let res = await get_sickbed_list(request_body)
        console.log('我负责的家庭病床列表', res);
        return {
            data: res.data.list,
            success: true,
            total: res.data.total_count,
        };
    }


    // 撤床按钮回调
    const isShowDelVisible = (
        status: boolean | ((prevState: boolean) => boolean),
        editId?: number,
    ) => {
        setDelVisible(status);
        setId(editId);
    };
    // 确认撤床的回调
    const delOk = async () => {
        let request_body = {
            id: id,
        };
        const res: any = await quash_sickbed(request_body);
        if (res.msg == 'ok') {
            message.success('撤床成功');
            actionRef.current?.reload();
        } else {
            message.error(res.msg);
        }
        setDelVisible(false);
    };

    // 列表字段
    const columns: ProColumns<GithubIssueItem>[] = [
        {
            title: '序号',
            key: 'index',
            dataIndex: 'index',
            valueType: 'index',
            width: 48,
        },
        {
            title: '病床编号',
            dataIndex: 'code',
            key: 'code',
            align: 'center',
        },
        {
            title: '患者',
            dataIndex: 'patient_name',
            key: 'patient_name',
            align: 'center',
        },
        {
            title: '性别',
            dataIndex: 'patient_sex',
            key: 'patient_sex',
            align: 'center',
            hideInSearch: true,
            valueEnum: {
                0: {
                    text: '未知',
                },
                1: {
                    text: '男',
                },
                2: {
                    text: '女',
                },
            },
        },
        {
            title: '创建人',
            key: 'manage_name',
            dataIndex: 'manage_name',
            align: 'center',
            hideInSearch: true,
        },
        {
            title: '创建时间',
            key: 'create_time',
            dataIndex: 'create_time',
            align: 'center',
            hideInSearch: true,
        },
        {
            title: '操作',
            valueType: 'option',
            align: 'center',
            key: 'option',
            render: (_, record) => (
                <>
                    <Button
                        key="info"
                        type="link"
                        onClick={() => {
                            setId(record.id)
                            // isShowDetailModal(true)
                            isShowInfoModal(true)

                        }}
                        disabled={record.patient_name ? false : true}
                    >
                        详情
                    </Button>
                    <Button
                        key="del"
                        type="link"
                        onClick={() => {
                            isShowDelVisible(true, record.id);
                        }}
                        disabled={record.patient_name ? false : true}
                    >
                        撤床
                    </Button>
                </>
            )


        },
    ];
    return (
        <PageContainer breadcrumb={false}>
            <ProTable<GithubIssueItem>
                columns={columns}
                actionRef={actionRef}
                cardBordered
                request={async (params = {}) => getMySickBeds(params)}
                editable={{
                    type: 'multiple',
                }}
                rowKey={(record) => record.id}
                search={{
                    labelWidth: 'auto',
                    collapseRender: false,
                    collapsed: false,
                    span: 6,
                }}
                options={{
                    setting: {
                        listsHeight: 400,
                    },
                }}
                pagination={{
                    pageSize: 10,
                }}
                dateFormatter="string"
                headerTitle="我负责的病床列表"
            />
            {/* 病床详情模态框 */}
            {isInfoModalOpen ? <SickbedsInfo isInfoModalOpen={isInfoModalOpen} isShowInfoModal={isShowInfoModal} actionRef={actionRef} id={id}></SickbedsInfo> : ""}
            {/* 撤床 */}
            <Modal
                title="撤床确认"
                open={delVisible}
                onOk={delOk}
                onCancel={() => isShowDelVisible(false)}
            >
                <Divider />
                <p>
                    <InfoCircleTwoTone twoToneColor="#ffcc00" /> 确定要进行撤床操作吗？
                </p>
            </Modal>
        </PageContainer>
    )
}
export default MySickbeds